﻿//=====================================================================================================
// LocalizationLibrary
// The Localization Library is a collection of reusable software components that provide 
// support for localization. This library enables you to localize WPF, Silverlight and WP7 applications.
// (http://localizationlibrary.codeplex.com)
//=====================================================================================================
// Author: Nicola Gallo (Website: www.nicola.gallo.name, Email: nicolagmt@hotmail.it) 
//=====================================================================================================

namespace ReflectionStudio.LocalizationLibrary
{
    //.NET References
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// Implements a generic language class.
    /// </summary>
    public class Language
    {
        //Constructors

        /// <summary>
        /// Creates a new instance of <see cref="Language"/> class.
        /// </summary>
        /// <param name="desc">Language's description.</param>
        /// <param name="code">Language's code</param>
        public Language(String desc, String code)
            : this(desc, code, false) { }

        /// <summary>
        /// Creates a new instance of <see cref="Language"/> class.
        /// </summary>
        /// <param name="desc">Language's description.</param>
        /// <param name="code">Language's code</param>
        /// <exception cref="System.ArgumentException">
        /// Thrown when <paramref name="code"/> parameter is equals to null.
        /// </exception>
        public Language(String desc, String code, bool rightToLeft)
        {
            if (String.IsNullOrEmpty(code))
            {
                throw new ArgumentException();
            }
            this.Description = desc;
            this.Code = code;
            this.RightToLeft = rightToLeft;
        }

        //Overloadings

        /// <summary>
        /// Constructor's explicit overloading of Language class.
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentException">
        /// Thrown if  str parameter is equals to empty string.
        /// </exception>
        public static explicit operator Language(String str)
        {
            if (String.IsNullOrEmpty(str))
            {
                throw new ArgumentException();
            }
            return Languages.FromString(str);
        }

        //Properties

        /// <summary>
        /// Gets the default language.
        /// </summary>
        public static Language DefaultLanguage 
        {
            get 
            {
                return (Language)"en";
            }
        }

        /// <summary>
        /// Language's description.
        /// </summary>
        public String Description { get; private set; }

        /// <summary>
        /// Language's code.
        /// </summary>
        public String Code { get; private set; }

        /// <summary>
        /// Language's position.
        /// </summary>
        public bool RightToLeft { get; private set; }

        //Methods

        /// <summary>
        /// Serves as a hash function for <see cref="ResourceLocalizedAttribute"/> type.
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return this.Code.GetHashCode();
        }

        /// <summary>
        /// Determines whether the specified <see cref="ResourceLocalizedAttribute"/> is equals to the current <see cref="ResourceLocalizedAttribute"/>.
        /// </summary>
        /// <param name="obj">Instance.</param>
        /// <returns>True whether the input instance is equals to the current instance otherwise false.</returns>
        public override bool Equals(object obj)
        {
            if (this == obj)
            {
                return true;
            }
            else if ((obj == null) || (obj.GetType() != this.GetType()))
            {
                return false;
            }
            Language instance = obj as Language;
            return instance.Code.Equals(this.Code);
        }

        /// <summary>
        /// Returns a <see cref="String"/> that represents the current <see cref="Language"/>.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return this.Description;
        }

        /// <summary>
        /// Validate the input <paramref name="lang"/>.
        /// </summary>
        /// <param name="language">Language.</param>
        /// <returns>Returns True whether the input <paramref name="lang"/> is a valid language's code, False otherwise.</returns>
        public static bool Validate(String language) 
        {
            Language lang = (Language)language;
            return (lang != null);
        }
    }
}